package com.ws.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ws.entity.GoodsType;
import com.ws.mapper.GoodsTypeMapper;
import com.ws.service.GoodsTypeService;
import com.ws.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;
    @Override
    public Result getGoodsTypeListforPage(int pno, int psize, String name) {

        Page<GoodsType> p = new Page<>(pno,psize);
        Page<GoodsType> resultPage = goodsTypeMapper.getGoodsTypeForPage(p, name);

        //从分页中提取数据
        List<GoodsType> goodsTypeList = resultPage.getRecords();
        JSONObject data = new JSONObject();
        data.put("list",goodsTypeList);

        //传入分页相关参数
        JSONObject page = new JSONObject();
        page.put("pno",pno);
        page.put("psize",psize);
        page.put("pCount",resultPage.getPages());
        page.put("totalElements",resultPage.getTotal());
        data.put("page",page);
        return Result.end(200,data,"查询成功");
    }

    @Override
    public Result insertGoodsType(GoodsType goodsType) {
        int count = goodsTypeMapper.insert(goodsType);
        if(count > 0) {
            return Result.end(200,"","增加成功");
        }
        return Result.end(500,"","增加失败");
    }

    @Override
    public Result findGoodsTypeById(Long id) {
        GoodsType goodsType = goodsTypeMapper.selectById(id);
        return Result.end(200,goodsType,"查询成功");
    }

    @Override
    public Result updateGoodsType(GoodsType goodsType) {
        int count = goodsTypeMapper.updateById(goodsType);
        if(count > 0) {
            return Result.end(200,"","修改成功");
        }
        return Result.end(500,"","修改失败");
    }

    @Override
    public Result deleteGoodsTypeById(Long id) {
        int count = goodsTypeMapper.deleteById(id);
        if(count > 0) {
            return Result.end(200,"","删除成功");
        }
        return Result.end(500,"","删除失败");
    }

    @Override
    public Result getGoodsTypeAll() {
        List<GoodsType> goodsTypeList = goodsTypeMapper.selectList(null);
        JSONObject data = new JSONObject();
        data.put("list",goodsTypeList);
        return Result.end(200,data,"查询成功");
    }
}
